package com.stu.wgt.test.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stu.wgt.test.bean.Address;
import com.stu.wgt.test.bean.Member;
import com.stu.wgt.test.mapper.AddressMapper;
import com.stu.wgt.test.mapper.MemberMapper;
import com.stu.wgt.test.service.AddressService;
import com.stu.wgt.test.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {
    @Autowired
    AddressMapper addressMapper;

    @Override
    public int insert(Address address) {
        return addressMapper.insert(address);
    }

    @Override
    public int delete(int id) {
        return addressMapper.deleteById(id);
    }

    @Override
    public int update(Address address) {
        QueryWrapper<Address> qw = new QueryWrapper<>();
        qw.eq("id", address.getId());
        return addressMapper.update(address, qw);
    }

    @Override
    public List<Address> selectAllAddress() {
        return addressMapper.selectList(null);
    }

    @Override
    public List<Address> selectById(int id) {
        QueryWrapper<Address> qw = new QueryWrapper<>();
        qw.eq("id", id);
        return addressMapper.selectList(qw);
    }

    @Override
    public List<Address> selectByUserId(Integer id) {
        QueryWrapper<Address> qw = new QueryWrapper<>();
        qw.eq("userid", id);
        return addressMapper.selectList(qw);
    }

    @Override
    public List<Address> selectByName(String username) {
        return null;
    }

    @Override
    public IPage<Address> searchAddress(String query, int pageNum, int pageSize) {
        Page<Address> page = new Page<>(pageNum, pageSize); // 创建分页对象，指定当前页码和每页显示条数
        QueryWrapper<Address> qw = new QueryWrapper<>(); // 创建查询条件对象
        qw.like("address", query); // 设置查询条件，这里使用模糊查询
        IPage<Address> memberPage = addressMapper.selectPage(page, qw); // 执行分页查询
        return memberPage;
    }
}
